#include "chooselevelscene.h"
#include <QLabel>
ChooseLevelScene::ChooseLevelScene(QWidget *parent)
    : QMainWindow{parent}
{
    // 设置固定大小
    this->setFixedSize(320, 588);

    // 设置标题
    this->setWindowTitle("选择关卡");

    // 设置图片
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    // 创建菜单栏
    QMenuBar *menu_bar = menuBar();
    this->setMenuBar(menu_bar);

    // 创建开始菜单
    QMenu *start_menu = menu_bar->addMenu("开始");

    // 创建菜单项
    QAction *quit_action = start_menu->addAction("退出");

    // 点击quit退出游戏
    connect(quit_action, &QAction::triggered, [=](){
        this->close();
    });

    // 返回按钮创建
    MyPushButton *back_btn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    back_btn->setParent(this);
    back_btn->move( this->width() - back_btn->width(), this->height() -back_btn->height());

    connect(back_btn, &MyPushButton::clicked, [=](){
        // 返回主场景
//        qDebug() << "返回主场景";
        // 延时
        QTimer::singleShot(500, this, [=](){
            // 隐藏自身
            emit this->chooseSceneBack();
        });
    });

    // 选择关卡按钮

    for (int i = 0; i < 20; i++)
    {
        MyPushButton *menu_btn = new MyPushButton(":/res/LevelIcon.png");
        menu_btn->setParent(this);
        menu_btn->move(25 + (i%4) * 70, 130 + (i/4) * 70);

        connect(menu_btn, &MyPushButton::clicked, [=](){
            QString str = QString("您选择的是 %1 关").arg(i+1);
            qDebug() << str;
        });

        QLabel *label = new QLabel(this);
        // 设置标签大小
        label->setFixedSize(menu_btn->width(), menu_btn->height());
        // 设置显示内容
        label->setText(QString::number(i+1));
        label->move(25 + (i%4) * 70, 130 + (i/4) * 70);
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        // 设置鼠标穿透
        label->setAttribute(Qt::WA_TransparentForMouseEvents); // 51鼠标穿透属性
    }
}

void ChooseLevelScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);

    // 加载标题
    pix.load(":/res/Title.png");
    painter.drawPixmap( (this->width() - pix.width()) * 0.5, 30, pix.width(), pix.height(), pix);
}

